package com.leyu.mall.site.member.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.leyu.mall.domain.member.UserBase;
import com.leyu.mall.service.member.IUserBaseService;
@Controller
@Scope(value = "prototype")
@RequestMapping("")
public class LoginController
{
	 
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login(Model model,UserBase userBase)
	{
		 
		return "user/login.html";
	}
	
	@RequestMapping(value = "register", method = RequestMethod.GET)
	public String register(Model model,UserBase userBase)
	{
	 
		return "user/login.html";
	}
	
	@RequestMapping(value = "iflogin", method = RequestMethod.POST)
	public String iflogin(Model model,UserBase userBase, HttpServletRequest request)
	{
		UserBase loginUser = userBaseServiceImpl.ifLogin(userBase);
 
		
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+loginUser);
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+loginUser.getUserName());
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+loginUser.getPassword());
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+loginUser.getUserId());
		
		
		if(loginUser!=null){
			request.getSession().setAttribute("loginSessionUser", loginUser);
			 return "redirect:/index";
		}else {
			return "user/login.html";	
		}
		
	}
	

	@RequestMapping(value = "registersave", method = RequestMethod.POST)
	public String registersave(Model model,UserBase userBase)
	{
		this.userBaseServiceImpl.addUserBase(userBase);
		 return "redirect:/index";
	}

	

	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(Model model,HttpServletRequest request)
	{
		request.getSession().removeAttribute("loginSessionUser");
		request.getSession().invalidate();   
		 
		 return "redirect:/index";
		 
	}
	
	
	@Autowired
	private IUserBaseService userBaseServiceImpl;
	
}
